package com.dusk.util
{
	import com.dusk.game.Logger;
	import com.dusk.net.AssetManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;
	
	public class ResUtil extends UtilBase
	{
		public function ResUtil()
		{
			super();
		}
		
		/**
		 * @param link 传入字符串，获取新的类（可实例化）
		 * @return
		 *
		 */
		public static function getNewObj(link:String):*
		{
			if(ApplicationDomain.currentDomain.hasDefinition(link))
			{
				var cls:Class = ApplicationDomain.currentDomain.getDefinition(link) as Class;
				return new cls();
			}
			var val:* = AssetManager.getIns().getClassInstance(link);
			if(!val)
			{
				Logger.logError(link + "定义未找到");
				return null;
			}
			return val;
		}
		
		public static function getImageBitmap(param1:String):Bitmap
		{
			var bmd:BitmapData = getNewObj(param1);
			if(!bmd) return null;
			return new Bitmap(bmd);
		}
		
		public static function getImageSprite(param1:String):Sprite
		{
			var bm:Bitmap = getImageBitmap(param1);
			var sprite:Sprite = new Sprite();
			sprite.addChild(bm);
			return sprite;
		}
		
		public static function getComponentView(name:String):DisplayObject
		{
			return getNewObj(getComponentViewKey(name));
		}
		
		public static function getComponentViewKey(name:String):String
		{
			return "gameComponent." + name;
		}
		
		public static function getLayerView(name:String):DisplayObject
		{
			return getNewObj(getLayerViewKey(name));
		}
		
		public static function getLayerViewKey(name:String):String
		{
			return "gameLayer." + name;
		}
	}
}
